var cheerio = require('cheerio');
var fs = require('fs');
var lib = require('../../lib/index.js');
var config = require('./detailConfig.json');

// 1.创建数据链接
var connection = lib.connection_tradechina();
connection.on('error', function(err) {
    console.log('db error', err);
    if (err.code === 'PROTOCOL_CONNECTION_LOST') {
        connection = lib.connection_tradechina();
    } else {
        throw err;
    }
});

function saveConfig(isAutoSave) {
    if(isAutoSave){
        fs.writeFile('./detailConfig.json', JSON.stringify(config), function(err) {
            if (err) {
                console.log(err);
            }else{
                console.log('config save ok');
            }
        });
    }else{
        console.log('中止程序,准备重启');
        fs.writeFile('./detailConfig.json', JSON.stringify(config), function(err) {
            if (err) {
                console.log(err);
            }else{
                console.log('中止程序前配置文件保存成功');
                process.exit(0);//主动中止程序
            }
        });
    }
}

var CATEGORIES;
function getTableName() {
    return 'tradechina'+config.categoryIndex;
}

async function getList(category) {
    // await lib.wait(1000);
    var params = {
        data:{
            cateId: category.id,
            limit: 50,
            page: config.page,
            searchKey: "",
            siteId: "2e16e44aa985434695954aba589cddbe"
        },
        lang: "en",
        time: 1597193156296,
        token: ""
    };
    var result = await lib.myRequest('https://global-all-api.tradechina.com/search/searchGoods.json',{
        method:'post',
        data:JSON.stringify(params),
        headers:{
            'content-type':'application/json'
        },
        transformRequest:null
    }).catch(function (err) {
        console.log('列表请求出错');
        console.log(err);
        return false;
    });
    if(!result){
        saveConfig();
        return false;
    }

    console.log('列表采集开始');
    console.log('time:'+lib.getTime());
    console.log('当前分类:'+config.categoryIndex);
    console.log('当前列表页:'+config.page);
    console.log('当前列表最大页数:'+result.data.totalPage);

    // var fullCategory = [];
    // result.data.cates.map(function (item) {
    //     fullCategory.push(item.cateName)
    // });

    var promiseArr = [];
    result.data.dtos.map(function (item,index) {
        // if(index == 0){
            var promiseFun = new Promise((resolve, reject) => {
                setTimeout(function () {
                    var params = {
                        index:index,
                        // fullCategory:fullCategory.toString(),
                        id:item.id,
                        categoryId:category.id,
                    };

                    getDetail(params,resolve,reject);
                }, config.detailTimeout*index);
            });

            promiseArr.push(promiseFun);
        // }
    });
    // return false;

    Promise.all(promiseArr).then((values) => {
        var errorList = [];//是否重启
        for(var i=0;i<values.length;i++){
            var item = values[i];
            if(item.code == -1){
                errorList.push(item.href);
            }
        }

        // console.log(values);
        // console.log(errorList);
        // 重启
        if(errorList.length > values.length/2){
            console.log('出错信息太多');
            saveConfig();
            return false;
        }
        // return false;
        // 创建mdc supply表
        lib.creditTradechinaProductTable(connection,getTableName());
        for(var i=0;i<values.length;i++){
            var item = values[i];
            if(item.code == 200){
                console.log(item.index + ' ---- ok');

                // 插入数据
                lib.insertData(connection,getTableName(),item.data);
            }else{
                console.log(item.index + ' ---- err');
                console.log(item);
            }
        }
        // return false;

        // 当前分类下一页
        config.page++;
        if(config.page < result.data.totalPage){
            saveConfig(true);
            console.log('');
            console.log('');
        }else{
            console.log('当前分类采集结束');
            saveConfig(true);
            console.log('');
            console.log('');
            // 下一分类列表
            config.categoryIndex++;
            config.page = 0;
        }
        if(config.categoryIndex > CATEGORIES.length){
            console.log();
            console.log();
            console.log('end');
        }else{
            var category = CATEGORIES[config.categoryIndex];
            // console.log(category);
            getList(category);
        }
    }).catch((e)=>{
        console.log('promise.all Err');
        // console.log(e);
    });
}

async function getDetail(obj,resolve,reject) {
    var detailUrl = 'https://www.tradechina.com/all/product/'+obj.id+'/en?id='+obj.categoryId;
    // 1.公司详情导航
    var result = await lib.myRequest(detailUrl,{
        // headers:{
        //     cookie:config.cookie
        // }
    }).catch(function (err) {
        console.log('内容url请求出错:'+detailUrl);
        // console.log(err);
        // saveConfig();
        return false;
    });
    if(!result){
        resolve({
            code:-1,
            index:obj.index,
        })
        return false;
    }
    var $ = cheerio.load(result);
    var jsString = $('script').eq(1).html();
    // console.log(eval(window={}));

    try {
        var start = jsString.indexOf('window.__INIT_STATE__=');
        var end = jsString.indexOf('window.deployPath=');
        jsString = JSON.parse(jsString.substring(start+22,end-1));
        var detail = jsString['/all/product/'+obj.id+'/en_ProductDetail'];

        var temp = {
            title:detail.data.productAbout
        };
        delete detail.data.productAbout;
        delete detail.data.companyInformation.contacts;
        for(var i in detail.data){
            temp[i] = JSON.stringify(detail.data[i])
        }
        // console.log(temp);

        resolve({
            code:200,
            index:obj.index,
            data:temp
        });
    } catch (e) {
        resolve({
            code:-1,
            index:obj.index,
        });
    }

}

async function application() {
    var result = await lib.myRequest('https://res.tradechina.com/api/1001/en/allCategory:.json').catch(function (err) {
        console.log('目录请求出错');
        console.log(err);
    });
    // console.log(result);

    CATEGORIES = [];
    result.map(function (item) {
        if(item.children){
            item.children.map(function (item2) {
                if(item2.children){
                    item2.children.map(function (item3) {
                        CATEGORIES.push({
                            id:item3.id,
                            name:item3.name
                        })
                    });
                }
            });
        }
    });
    console.log(CATEGORIES.length);
    var category = CATEGORIES[config.categoryIndex];
    // console.log(category);
    getList(category);
}
application();
